package com.bdi.mqtt_broker.service.impl;

import com.bdi.mqtt_broker.service.ChannelManagerService;
import io.netty.channel.ChannelId;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author WangNing yogehaoren@gmial.com <br>
 * @since 1.0
 * 基于内存实现的ChannelIdMap
 * 集群模式可以使用Redis存储ChannelId
 */
@Service
public class ChannelManagerImpl implements ChannelManagerService {

    private static final Map<String, ChannelId> CHANNEL_MAP = new HashMap<>();


    @Override
    public ChannelId get(String key) {
        return CHANNEL_MAP.getOrDefault(key, null);
    }

    @Override
    public void put(String key, ChannelId channelId) {
        CHANNEL_MAP.put(key, channelId);
    }

    @Override
    public void remove(String key) {
        CHANNEL_MAP.remove(key);
    }
}
